#ifndef SERVER_WRAPPER_HPP
#define SERVER_WRAPPER_HPP
#include <unordered_map>
#include <string>
#include <signal.h>
#include <memory>
#include <iomanip>
#include <sstream>
#include "oj_server.hpp"
#include "oj_controller.hpp"
#include "../comm/httplib.h"
#include "../comm/util.hpp"
#include "../comm/global.hpp"
// #include "../comm/ThreadPool.hpp"
#include "../managers/userManager.hpp"
#include "../managers/articleManager.hpp"
#include "../managers/videoManager.hpp"
#include "../managers/commentManager.hpp"
#include "../managers/postManager.hpp"

using namespace ns_controller;
using namespace ns_users;

class ServerWrapper {
public:
    ServerWrapper();
    void start(int port);
    void stop();
    static void Recovery(int sig);

private:
    std::shared_ptr<httplib::Server> server;
    std::unordered_map<std::string, std::string> session_store;
    std::unique_ptr<Controller> ctrl_ptr;
    std::shared_ptr<ArticleManager> _article;
    std::shared_ptr<VideoManager> _video;
    std::shared_ptr<CommentManager> _comment;
    std::shared_ptr<PostManager> _post;
    std::atomic<bool> running{true};  // 用于控制服务器运行状态
    // std::shared_ptr<comm::ThreadPool> _threadPool;  // 线程池
    Users user;

    void initRoutes();
    void listenForQuitCommand();  // 用于监听退出命令

    // 服务器处理函数
    inline int getUserId(const httplib::Request &req);
    void handleLogin(const httplib::Request &req, httplib::Response &resp);
    void handleRegister(const httplib::Request &req, httplib::Response &resp);
    void handleLogout(const httplib::Request &req, httplib::Response &resp);
    void handleLogOff(const httplib::Request &req, httplib::Response &resp);
    void handleUpdateUserInfo(const httplib::Request &req, httplib::Response &resp);
    void handleUserInfo(const httplib::Request &req, httplib::Response &resp);
    void handleSubmitApplication(const httplib::Request &req, httplib::Response &resp);
    void handleAllQuestions(const httplib::Request &req, httplib::Response &resp);
    void handleSingleQuestion(const httplib::Request &req, httplib::Response &resp);
    void handleJudge(const httplib::Request &req, httplib::Response &resp);
    void handleAIChat(const httplib::Request &req, httplib::Response &resp);
    void handleAddArticle(const httplib::Request &req, httplib::Response &resp);
    void handleGetArticles(const httplib::Request &req, httplib::Response &resp);
    void handleGetArticleById(const httplib::Request &req, httplib::Response &resp);
    void handleGetVideos(const httplib::Request &req, httplib::Response &resp);
    void handleCheckLoginStatus(const httplib::Request &req, httplib::Response &resp);
    void handleAddComment(const httplib::Request &req, httplib::Response &resp);
    void handleGetComments(const httplib::Request &req, httplib::Response &resp);
    void handleIncreaseViewCount(const httplib::Request &req, httplib::Response &resp);
    void handleLikeArticle(const httplib::Request &req, httplib::Response &resp);
    void handleAddPost(const httplib::Request &req, httplib::Response &resp);
    void handleGetPosts(const httplib::Request &req, httplib::Response &resp);
    void handleDeleteArticle(const httplib::Request &req, httplib::Response &resp);
};

#endif // SERVER_WRAPPER_HPP
